package result;
import idx.Extractor;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import ICTCLAS.I3S.AC.ICTCLAS50;

public class DictBuilder {
	public static void main(String [] a) throws IOException
	{
		System.out.println(FeatureExtractor.EditDistance("不是", "不是"));
	}
	Extractor ex;
	FileWriter w;
	public DictBuilder() throws IOException
	{
		ex = new Extractor();
		ex.buildConvertTable("table.txt");
		w = new FileWriter("user.dic");
	}
	void recursiveAdd(String path) throws IOException
	{
		File f = new File(path);
		File sub[] = f.listFiles();
		for(int i = 0; i < sub.length; i++)
		{
			//System.out.println(ex.Convert(sub[i].getAbsolutePath()).substring(28));
			if(sub[i].isDirectory())
				recursiveAdd(sub[i].getAbsolutePath());
			else
			{
				if(!sub[i].getName().endsWith(".ext"))
					continue;
				if(sub[i].getName().charAt(0) <= 'Z' && sub[i].getName().charAt(0) >= 'A')
					continue;
				String docName = ex.Convert(sub[i].getName().substring(0, sub[i].getName().indexOf(".")));
				if(docName.length() == 1 || docName.length() > 6)
					continue;
				//System.out.println(docName);
				if(imTag(docName)) //add to user dict or not
				{
					w.write(docName + "@@n\n");
					//w.flush();
				}
			}
		}
		if(path.equals("/home/antony/Documents/wiki/zh/articles"))
			w.close();
		return;
	}
	boolean imTag(String a) throws IOException
	{
		ICTCLAS50 testICTCLAS50 = new ICTCLAS50();
		String argu = ".";
		//初始化
		if (testICTCLAS50.ICTCLAS_Init(argu.getBytes("UTF-8")) == false)
		{
			System.out.println("Init Fail!");
			return false;
		}


		//设置词性标注集(0 计算所二级标注集，1 计算所一级标注集，2 北大二级标注集，3 北大一级标注集)
		testICTCLAS50.ICTCLAS_SetPOSmap(0);


		//导入用户词典前分词
		byte nativeBytes[] = testICTCLAS50.ICTCLAS_ParagraphProcess(a.getBytes("UTF-8"), 0, 1);//分词处理
		//System.out.println(nativeBytes.length);
		String nativeStr = new String(nativeBytes, 0, nativeBytes.length, "UTF-8");
		String [] words = nativeStr.split(" ");
		if(words.length > 1)
			return true;
		if(words[0].split("/").length < 2)
			return true;
		if(words[0].split("/")[1].startsWith("ns") || words[0].split("/")[1].startsWith("nr") ||
				words[0].split("/")[1].startsWith("t") || words[0].split("/")[1].startsWith("m"))
			return false;
		return true;
	}
}
